package net.xpclouds.test;

import com.alibaba.fastjson.JSON;
import io.github.dv996coding.service.PrintService;
import io.github.dv996coding.util.ImageUtil;
import io.github.dv996coding.vo.*;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;

@SpringBootTest(classes = XpcloudsTestproApplication.class)
public class XpOtherApi {
    @Resource
    private PrintService printService;

    /**
     * 查询打印订单状态
     * 为了测试方便此处使用get请求，在实际生产开发中，以自己的项目规范要求为准
     */
    @Test
    public void testQueryOrderState() {
        ObjectRestResponse<Boolean> restResponse = printService.queryOrderState(new PrintOrderStatusRequest("<yourPrintOderNo>"));
        System.out.println(JSON.toJSONString(restResponse));
    }

    /**
     * 查询打印机状态
     * 为了测试方便此处使用get请求，在实际生产开发中，以自己的项目规范要求为准
     */
    @Test
    void testQueryPrinterStatus() {
        ObjectRestResponse restResponse = printService.queryPrinterStatus(new PrinterVoRequest("<yourPrinterSn>"));
        System.out.println(JSON.toJSONString(restResponse));
        if (restResponse.getData() != null) {
            PrinterStatusResponse statusResponse = JSON.parseObject(restResponse.getData().toString(), PrinterStatusResponse.class);
            System.out.println(statusResponse.getVersion());
        }
    }

    /**
     * 查询待打印任务数
     * 为了测试方便此处使用get请求，在实际生产开发中，以自己的项目规范要求为准
     */
    @Test
    void testGetAwaitTasks() {
        ObjectRestResponse<Integer> restResponse = printService.getAwaitTasks(new PrinterVoRequest("<yourPrinterSn>"));
        System.out.println(JSON.toJSONString(restResponse));
    }

    /**
     * 发送打印机天猫精灵语音操作指令响应
     */
    @Test
    void testSendTmallSpiritVoice() throws IOException {
        String sn = "<yourPrinterSn>";
        BufferedImage image = ImageUtil.binaryImage(ImageIO.read(new File("D:\\测试项目\\item\\new123.jpg")));
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        ImageIO.write(image, "png", stream);
        final byte[] data = stream.toByteArray();
        stream.close();
        //构造打印接口参数
        String str = Base64.getEncoder().encodeToString(data);
        PrintOrderRequest printOrderRequest = new PrintOrderRequest(sn, str);
        printOrderRequest.setPaperWidth(880);
        //调用打印接口，打印订单数据
        ObjectRestResponse<String> restResponse = printService.sendTmallSpiritVoice(printOrderRequest);
        System.out.println(JSON.toJSONString(restResponse));
    }
}